package com.songtao.rabbitmq.simple;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.songtao.rabbitmq.config.RabbitConfig;
import com.songtao.rabbitmq.util.RabbitConnectionUtils;

public class Recv {
    
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //新的连接
        Connection connection = RabbitConnectionUtils.getConnection();
        //新的频道
        final Channel channel = connection.createChannel();
        //定义消费者
        Consumer consumer = new DefaultConsumer(channel){
            //消费者处理消息
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, 
                    BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("recv message:"+new String(body));
            }
        };
        //监听消息
        channel.basicConsume(RabbitConfig.QUEUE_FIRST,true,consumer);        
        
        
    }
}
